// let conf = {
// 	ur: "http://" + window.location.host,
// 	storage: window.localStorage
// };
let conf = {
	ur: "http://localhost:8080",
	storage: window.localStorage
};
let userToken = conf.storage.getItem("userToken");

let app = new Vue({
	el: '#app',
	data: {
		loginHint: '',
		isLogin: false,
		sysUser: {
			"username": ""
		},
		userRole: '',
		username: '',
		password: '',
		regHint: '',
		regUsername: '',
		regPwd: '',
		regConfPwd: '',
		regEmail: '',
		regMobile: '',
		regRole: 2
	},
	mounted: function() {
		if (conf.storage.getItem("userToken") !== "" && conf.storage.getItem("userToken") !== undefined && conf.storage.getItem(
				"userToken") !== null) {
			this.isLogin = true;
			this.sysUser = JSON.parse(window.localStorage.getItem("sysUser"));
			switch (conf.storage.getItem("userPermission")) {
				case "0":
					this.userRole = "管理员";
					break;
				case "1":
					this.userRole = "企业用户";
					break;
				case "2":
					this.userRole = "求职者";
					break;
			}
		}
	},
	methods: {
		doLogin: function() {
			let userName = this.username;
			let passWord = this.password;
			if (userName.trim() === "") {
				this.loginHint = "用户名不能为空！";
				return;
			} else if (passWord.trim() === "") {
				this.loginHint = "密码不能为空！";
				return;
			}
			$.ajax({
				url: conf.ur + "/doLogin",
				type: 'post',
				dataType: 'json',
				data: JSON.stringify({
					"username": userName,
					"password": passWord
				}),
				headers: {
					"Content-Type": "application/json"
				},
				success: (data) => {
					console.log(JSON.stringify(data));
					switch (data.statusCodeValue) {
						case 200:
							conf.storage.setItem("userToken", data.x_auth_token);
							conf.storage.setItem("userPermission", data.permission);
							conf.storage.setItem("sysUser", JSON.stringify(data.sysUser));
							window.location.reload();
							break;
						case 401:
							this.loginHint = "用户名或密码错误！";
							break;
						case 500:
							this.loginHint = "内部服务器错误！";
							break;
						default:
							this.loginHint = "未知错误！";
							break;
					}
				},
				error: (err) => {
					this.loginHint = "未知错误！";
					console.log(JSON.stringify(err));
				}
			});
		},
		doRegister: function() {
			let userName = this.regUsername;
			let passWord = this.regPwd;
			let confPwd = this.regConfPwd;
			if (userName.trim() === "") {
				this.regHint = "用户名不能为空！";
				return;
			} else if (passWord.trim() === "") {
				this.regHint = "密码不能为空！";
				return;
			} else if (confPwd.trim() === "") {
				this.regHint = "确认密码不能为空！";
				return;
			} else if (passWord.trim() !== confPwd.trim()) {
				this.regHint = "两次密码输入需要一致！";
				console.log(this.regRole)
				return;
			}
			$.ajax({
				url: conf.ur + "/doRegister",
				type: 'post',
				dataType: 'json',
				data: JSON.stringify({
					"username": userName,
					"password": passWord,
					"email": this.regEmail,
					"mobile": this.regMobile,
					"permission": this.regRole
				}),
				headers: {
					"Content-Type": "application/json"
				},
				success: (data) => {
					console.log(JSON.stringify(data));
					if (data.code === 0) {
						alert("注册成功！");
						window.location.reload();
					} else if (data.code === -1 && data.msg === "用户名已经存在！") {
						this.regHint = "用户名已经存在！";
					}
				},
				error: (err) => {
					this.regHint = "未知错误！";
					console.log(JSON.stringify(err));
				}
			});
		},
		logout: function() {
			let userToken = conf.storage.getItem("userToken");
			conf.storage.setItem("userToken", "");
			conf.storage.setItem("userPermission", "");
			conf.storage.setItem("sysUser", "");
			let purl = conf.ur + "/logout";
			$.ajax({
				url: purl,
				type: 'get',
				dataType: 'json',
				contentType: "application/json",
				headers: {
					"x-auth-token": userToken
				},
				complete: (xhr) => {
					window.location.reload();
				}
			});
		}
	}
})
